<template>
  <div class="c_captcha">
    <div class="box" :class="{
      slider: type === 1,
      image: type === 2,
      qrcode: type === 3
    }">
      <i class="boticon bot-close" title="关闭" v-if="type !== 1" @click="close('close')"></i>
      <iframe ref="iframe" :src="captchaUrl" frameborder="0"></iframe>
      <div v-if="type === 3" class="qrcode_btn">
        <el-button type="primary" @click="close('login')">登 录</el-button>
        <el-button type="info" @click="close('refresh')">刷 新</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "c_captcha",
  props: {
    type: {
      require: true,
      type: Number,
    },
    url: {
      require: true,
      type: String,
    },
  },
  computed: {
    captchaUrl() {
      let url = "";
      if (this.type === 1) {
        url = this.url
          .match(/https:\S+/)[0]
          .replace(
            /^https:\/\/ssl\.captcha\.qq\.com\/template\/wireless_mqq_captcha\.html\?/,
            "/captcha.html?"
          );
      } else {
        url = this.url;
      }
      return url;
    },
  },
  mounted() {
    window.onmessage = this.submit;
  },
  methods: {
    // 滑块验证码
    submit(ev) {
      if (ev.data.type === "submitTicket") {
        const ticket = ev.data.value;
        this.$emit("ok", ticket);
      }
    },
    // 关闭弹窗
    close(status) {
      this.$emit("ok", status);
    },
  },
};
</script>

<style lang="scss">
.c_captcha {
  position: fixed;
  top: 40px;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 9999;
  background: rgba($color: #000000, $alpha: 0.3);
  display: flex;
  align-items: center;
  justify-content: center;

  .box {
    padding: 5px;
    border-radius: 5px;
    background: #fff;
    box-sizing: content-box;
    position: relative;

    &.slider {
      width: 260px;
      height: 240px;
    }

    &.image {
      width: 460px;
      height: 300px;
    }

    &.qrcode {
      width: 180px;
      height: 200px;
      display: flex;
      justify-content: center;
      padding-top: 30px;
      position: relative;

      .bot-close {
        left: unset;
        top: 5px;
        right: 5px;
        z-index: 10;
      }

      iframe {
        width: 135px;
        height: 135px;
        transform: scale(1.2);
      }

      .qrcode_btn {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 10px;
        width: 100%;
        text-align: center;

        &::before {
          content: "请使用 手机QQ 扫描二维码";
          font-size: 12px;
          transform: scale(0.8);
          display: block;
          color: #999;
          position: relative;
          bottom: 5px;
        }
      }
    }

    .bot-close {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      top: 10px;
      left: 10px;
      width: 20px;
      height: 20px;
      cursor: pointer;
      border-radius: 50%;
      padding: 5px;
      box-sizing: content-box;
      transition: 0.2s;

      &:hover {
        background: rgba($color: #000000, $alpha: 0.1);
      }
    }

    iframe {
      width: 100%;
      height: 100%;
    }
  }
}
</style>